(function() {
	$(function() {
		var $swalOverlay = $("<div>", {
				"class": "sweet-overlay"
			}).appendTo($("body")),
			$swal = $("<div>", {
				"class": "sweet-alert showSweetAlert visible",
				"style": "margin-top:-162px;"
			}).appendTo($("body"));
		var $error = $("<div>", {
				"class": "sa-icon sa-error",
				"style": "display:none;"
			}).append($("<span>", {
				"class": "glyphicon glyphicon-remove-sign"
			})).appendTo($swal),
			$success = $("<div>", {
				"class": "sa-icon sa-success",
				"style": "display:none;"
			}).append($("<span>", {
				"class": "glyphicon glyphicon-ok-sign"
			})).appendTo($swal),
			$warning = $("<div>", {
				"class": "sa-icon sa-warning",
				"style": "display:none;"
			}).append($("<span>", {
				"class": "glyphicon glyphicon-exclamation-sign"
			})).appendTo($swal),
			$info = $("<div>", {
				"class": "sa-icon sa-info",
				"style": "display:none;"
			}).append($("<span>", {
				"class": "glyphicon glyphicon-info-sign"
			})).appendTo($swal),
			$title = $("<h2>").appendTo($swal),
			$text = $("<p>").appendTo($swal),
			$btnGroup = $("<div>", {
				"class": "sa-button-container"
			}).appendTo($swal);
		var $cancel = $("<button>", {
				"class": "cancel",
				"tabindex": "2"
			}).appendTo($btnGroup),
			$confirm = $("<button>", {
				"class": "confirm",
				"tabindex": "1"
			}).appendTo($("<div>", {
				"class": "sa-confirm-button-container"
			}).appendTo($btnGroup));

		function showSwal() {
			$swalOverlay.show().animate({
				opacity: 1
			}, 300);
			$swal.show()
		}

		function hideSwal() {
			/* $swalOverlay.animate({
				opacity: 0
			}, 300, function() {
				$(this).hide()
			}); */
			$swalOverlay.hide().css("opacity", "0");
			$swal.hide()
		}

		window.swal = function(options, callback) {
			options = $.extend({
				title: "",
				text: "",
				type: "",
				showConfimButton: true,
				showCancelButton: false,
				confirmButtonText: "确定",
				cancelButtonText: "取消",
				closeOnConfirm: true,
				closeOnCancel: true
			}, options);
			$error.hide();
			$success.hide();
			$warning.hide();
			$info.hide();
			switch (options.type) {
				case "success":
					$success.show();
					break;
				case "error":
					$error.show();
					break;
				case "warning":
					$warning.show();
					break
				case "info":
					$info.show();
					break;
			}
			$title.html(options.title || "");
			$text.html(options.text || "");
			options.showConfimButton ? $confirm.prop("disabled", null).show().html(options.confirmButtonText) : $confirm.hide();
			options.showCancelButton ? $cancel.prop("disabled", null).show().html(options.cancelButtonText) : $cancel.hide();
			$confirm.siblings(".la-ball-fall").remove();
			if (options.closeOnConfirm) {
				$confirm.off("click").on("click", hideSwal)
			} else {
				$confirm.off("click").on("click", function() {
					$(this).parent().append('<div class="la-ball-fall"><div></div><div></div><div></div></div>')
					$confirm.prop("disabled", true)
					$cancel.prop("disabled", true)
				})
			}
			if (options.closeOnCancel) {
				$cancel.off("click").on("click", hideSwal)
			}
			if (callback && $.isFunction(callback)) {
				$confirm.on("click", function() {
					callback(true)
				});
				$cancel.on("click", function() {
					callback(false)
				})
			}
			showSwal()
		}
		swal.prototype.close = hideSwal()
	})
})();
(function() {
	jQuery.extend({
		msg: function(text, type, time) {
			text = text || "";
			type = type || "";
			time = time || 2000;
			var $msg = $("<div>", {
				"class": "wm-msg"
			}).appendTo($("body"));
			var $msgBody = $("<div>", {
				"class": "has-shadow wm-msg-body"
			}).appendTo($msg);
			var iconObj = {
				"success": "text-success glyphicon glyphicon-ok-sign",
				"warning": "text-warning glyphicon glyphicon-exclamation-sign",
				"info": "text-info glyphicon glyphicon-info-sign",
				"error": "text-danger glyphicon glyphicon-remove-sign"
			}
			if (type && iconObj[type]) {
				$msgBody.append($("<span>", {
					"class": "mr15 " + iconObj[type]
				}));
			}
			$msgBody.append(text);

			setTimeout(showMsg, 0)

			function showMsg() {
				$msg.addClass("in");
				setTimeout(hideMsg, time);
			}

			function hideMsg() {
				$msg.addClass("out");
				setTimeout(function() {
					$msg.remove()
				}, 400);
			}
		},
		//警告框
		warning: function(text, type, time, closeBtn) {
			if (!text) {
				return false;
			};
			if (!type) {
				type = "warning";
			};
			if (!time && time != 0) {
				time = 2000;
			};
			var $box = $("body").children(".warning-box").length > 0 ? $("body").children(".warning-box") : $("<div>", {
				"class": "warning-box"
			}).appendTo($("body"));
			var $alert = $("<div>", {
				"class": "alert has-shadow alert-" + type + " transition slide",
				"role": "alert"
			}).html(text).appendTo($box);
			setTimeout(fadein, 0);

			function fadein() {
				$alert.addClass("in");
				setTimeout(remove, time);
			}

			function remove() {
				$alert.removeClass("in");
				setTimeout(function() {
					$alert.remove();
				}, 150);
			}
		},
	});

})();
